//
//  CityMenuView.m
//  AroundTheWorld
//
//  Created by dllo on 16/5/31.
//  Copyright © 2016年 IfelseGo an. All rights reserved.
//

#import "CityMenuView.h"
#define WIDTH [UIScreen mainScreen].bounds.size.width
#define HEIGHT [UIScreen mainScreen].bounds.size.height
#import "DataHandler.h"
#import "CityMenuTableViewCell.h"
#import "CityMenuTableDataViewCell.h"


static NSString *const cityListReuse = @"cityListReuse";
static NSString *const cityDataReuse = @"cityDataReuse";


@implementation CityMenuView
- (void) showinView
{
    self.backgroundColor = [UIColor clearColor];
    [self getData];
    
    UIView *backV = [[UIView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT)];
    backV.backgroundColor = [UIColor blackColor];
    backV.alpha = 0.3;
    [self addSubview:backV];
    
    self.myTabelView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, 130, HEIGHT - 250) style:UITableViewStylePlain];
    self.myTabelView.backgroundColor = [UIColor whiteColor];
    self.myTabelView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self addSubview:self.myTabelView];
    self.myTabelView.showsVerticalScrollIndicator = NO;
    
    self.myTabelDataView = [[UITableView alloc] initWithFrame:CGRectMake(130, 0, WIDTH - 130, HEIGHT - 250) style:UITableViewStylePlain];
    self.myTabelDataView.backgroundColor = [UIColor colorWithRed:240 / 255.0 green:240 / 255.0 blue:240 / 255.0  alpha:1.0];
    self.myTabelDataView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self addSubview:self.myTabelDataView];
    self.myTabelDataView.showsVerticalScrollIndicator = NO;
    
    //成为代理人
    self.myTabelView.delegate = self;
    self.myTabelView.dataSource = self;
    self.myTabelDataView.delegate = self;
    self.myTabelDataView.dataSource = self;
    
    //注册
    [self.myTabelView registerClass:[CityMenuTableViewCell class] forCellReuseIdentifier:cityListReuse];
    [self.myTabelDataView registerClass:[CityMenuTableDataViewCell class] forCellReuseIdentifier:cityDataReuse];
}
//data
-(void)getData{
    self.myArrayM = [NSMutableArray array];
    self.myCityArrayM = [NSMutableArray array];
    
    DataHandler *handler1 = [[DataHandler alloc] init];
    [handler1 getCommunityFindTravellMenuBase: ^(id result) {
        self.myArrayM = [NSMutableArray arrayWithArray:result];
        FindTravellingMenuModel *model = [self.myArrayM objectAtIndex:0];
        self.myCityArrayM = model.destination;
        self.indexpthRow = 0;
        FindTravellingMenuDestinationModel *dataModel = [self.myCityArrayM objectAtIndex:self.indexpthRow];
        self.myPlaceArrayM = dataModel.place;
        [self.myTabelView reloadData];
    } failedHandler:^(NSError *error) {
    }];
}

//cell高
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{

    return 60;
}

//cell数
- (NSInteger )tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (tableView == self.myTabelView) {
        return self.myCityArrayM.count;
    }
      return self.myPlaceArrayM.count;
}

//建cell
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (tableView == self.myTabelView) {
        CityMenuTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cityListReuse];
        FindTravellingMenuDestinationModel *model = [self.myCityArrayM objectAtIndex:indexPath.row];
        cell.myModel = model;
//        cell.textLabel.textAlignment = NSTextAlignmentCenter;
//        cell.selectionStyle =UITableViewCellSelectionStyleNone;

        return cell;
    }
   CityMenuTableDataViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cityDataReuse];
    FindTravellingMenuDestinationPlaceModel *dataModel = [self.myPlaceArrayM objectAtIndex:indexPath.row];
    cell.myModel = dataModel;
    cell.backgroundColor = [UIColor colorWithRed:247 / 255.0 green:245 / 255.0 blue:248 / 255.0 alpha:1.0];
    cell.selectionStyle =UITableViewCellSelectionStyleNone;
    return cell;
}

//点击方法
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (tableView == self.myTabelView) {
        self.indexpthRow = indexPath.row;
        FindTravellingMenuDestinationModel *dataModel = [self.myCityArrayM objectAtIndex:self.indexpthRow];
        self.myPlaceArrayM = dataModel.place;
        [self.myTabelDataView reloadData];
    }else {
    FindTravellingMenuDestinationPlaceModel *model =[self.myPlaceArrayM objectAtIndex:indexPath.row];
    //通知
    [[NSNotificationCenter defaultCenter] postNotificationName:@"CityName" object:model];
    }

}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
